#include "stdafx.h"  
#include <windows.h>  
#include <stdio.h>  
#include <stdlib.h>  
	   
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS  
#include <atlbase.h>  
#include <atlstr.h>  
#include "global.h"	   
#define USE_WINHTTP    //Comment this line to user wininet.  
#ifdef USE_WINHTTP  
#include <winhttp.h>  
#pragma comment(lib, "winhttp.lib")  
#else  
#include <wininet.h>  
#pragma comment(lib, "wininet.lib")  
#endif  
#define BUF_SIZE    (1024)  
 int WINHTTP(CString postdata) {
	LPSTR pszData = (char*)(LPCTSTR)postdata;
	DWORD dwBytesWritten = 0;
	BOOL  bResults = FALSE;
	HINTERNET hSession = NULL,
			  hConnect = NULL,
			  hRequest = NULL;

	// Use WinHttpOpen to obtain a session handle.
	hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", 
							 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
							 WINHTTP_NO_PROXY_NAME, 
							 WINHTTP_NO_PROXY_BYPASS, 0);

	// Specify an HTTP server.
	if (hSession)
		hConnect = WinHttpConnect( hSession, SERVERIP,
								   INTERNET_DEFAULT_HTTP_PORT, 0);

	// Create an HTTP Request handle.
	if (hConnect)
		hRequest = WinHttpOpenRequest( hConnect, L"POST", 
									   OPENOBJECTNAME_1, 
									   NULL, WINHTTP_NO_REFERER, 
									   WINHTTP_DEFAULT_ACCEPT_TYPES, 
									   0);

	// Send a Request.
	if (hRequest) 
		bResults = WinHttpSendRequest( hRequest, 
									   WINHTTP_NO_ADDITIONAL_HEADERS,
									   0, WINHTTP_NO_REQUEST_DATA, 0, 
									   (DWORD)strlen(pszData), 0);

	// Write data to the server.
	int data = (DWORD)strlen(pszData);
	if (bResults)
		bResults = WinHttpWriteData( hRequest, pszData, 
									 (DWORD)strlen(pszData), 
									 &dwBytesWritten);

	// End the request.
	if (bResults)
		bResults = WinHttpReceiveResponse( hRequest, NULL);

	// Report any errors.
	if (!bResults)
	{
		int error = GetLastError();
	    return 0;
	}


	// Close any open handles.
	if (hRequest) WinHttpCloseHandle(hRequest);
	if (hConnect) WinHttpCloseHandle(hConnect);
	if (hSession) WinHttpCloseHandle(hSession);
	return 1;
 }

 bool Winhttp_Getversion(char *str)
 {
  DWORD dwSize = 0;
  DWORD dwDownloaded = 0;
  LPSTR pszOutBuffer;
  BOOL  bResults = FALSE;
  HINTERNET  hSession = NULL, 
			 hConnect = NULL,
			 hRequest = NULL;

  // Use WinHttpOpen to obtain a session handle.
  hSession = WinHttpOpen( L"GetLatestVersion",  
						  WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
						  WINHTTP_NO_PROXY_NAME, 
						  WINHTTP_NO_PROXY_BYPASS, 0 );

  // Specify an HTTP server.
  if( hSession )
	hConnect = WinHttpConnect( hSession, SERVERIP,
							   INTERNET_DEFAULT_HTTP_PORT, 0 );

  // Create an HTTP request handle.
  if( hConnect )
	hRequest = WinHttpOpenRequest( hConnect, L"GET", OPENOBJECTNAME_2,
								   NULL, WINHTTP_NO_REFERER, 
								   WINHTTP_DEFAULT_ACCEPT_TYPES, 
								   0 );

  // Send a request.
  if( hRequest )
	bResults = WinHttpSendRequest( hRequest,
								   WINHTTP_NO_ADDITIONAL_HEADERS, 0,
								   WINHTTP_NO_REQUEST_DATA, 0, 
								   0, 0 );


  // End the request.
  if( bResults )
	bResults = WinHttpReceiveResponse( hRequest, NULL );

  // Keep checking for data until there is nothing left.
  if( bResults )
  {
	do 
	{
	  // Check for available data.
	  dwSize = 0;
	  if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
		printf( "Error %u in WinHttpQueryDataAvailable.\n",
				GetLastError( ) );
	  if( dwSize == 0 )
		  break;
	  // Allocate space for the buffer.
	  pszOutBuffer = new char[dwSize+1];
	  if( !pszOutBuffer )
	  {
		printf( "Out of memory\n" );
		dwSize=0;
	  }
	  else
	  {
		// Read the data.
		ZeroMemory( pszOutBuffer, dwSize+1 );

		if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
							  dwSize, &dwDownloaded ) )
		  printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
		else
		  printf( "%s", pszOutBuffer );

		// Free the memory allocated to the buffer.
		strcat(str,pszOutBuffer);
		delete [] pszOutBuffer;
	  }
	} while( dwSize > 0 );
  }


  // Report any errors.
  if( !bResults )
	return false;

  // Close any open handles.
  if( hRequest ) WinHttpCloseHandle( hRequest );
  if( hConnect ) WinHttpCloseHandle( hConnect );
  if( hSession ) WinHttpCloseHandle( hSession );

  return true;
 }
